package com.yc.cloud.openai.portal.controller;

import java.io.IOException;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.yc.cloud.common.basic.api.ResponseResult;
import com.yc.cloud.common.oss.vo.OssFileVo;
import com.yc.cloud.openai.portal.dto.PromptDto;
import com.yc.cloud.openai.portal.dto.request.TextToVideoRequest;
import com.yc.cloud.openai.portal.dto.request.VideoRemoveShareWaterMarkParam;
import com.yc.cloud.openai.portal.dto.request.VideoRemoveWaterMarkParam;
import com.yc.cloud.openai.portal.dto.video.request.ImageToVideoRequest;
import com.yc.cloud.openai.portal.service.VideoHelperService;
import com.yc.cloud.openai.portal.service.VideoService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.val;
import lombok.extern.slf4j.Slf4j;

/**
 * AIGC/视频模块
 */
@RestController
@RequestMapping("video")
@Api(value = "视频模块", tags = "视频模块")
@Slf4j
public class VideoController {
    private final VideoHelperService videoHelperService;
    private final VideoService videoService;

    public VideoController(VideoHelperService videoHelperService, VideoService videoService) {
        this.videoHelperService = videoHelperService;
        this.videoService = videoService;
    }

    @PostMapping("/remove-watermark/share")
    @ApiOperation(value = "视频分享链接去水印", notes = "用户选出视频文件，选择去水印的位置,后台将去除水印并返回去水印后的视频文件")
    public ResponseResult<OssFileVo> removeShareWatermark(
            @Validated @RequestBody VideoRemoveShareWaterMarkParam param) {
        val result = videoHelperService.removeShareWatermark(param);
        return ResponseResult.success(result);
    }

    @PostMapping("/remove-watermark")
    @ApiOperation(value = "视频去水印", notes = "用户选出视频文件，选择去水印的位置,后台将去除水印并返回去水印后的视频文件")
    public ResponseResult<OssFileVo> removeWatermark(@Validated @RequestBody VideoRemoveWaterMarkParam param)
            throws IOException {
        val startTime = System.currentTimeMillis();
        val result = videoHelperService.removeWatermark(param);
        val endTime = System.currentTimeMillis();
        log.info("视频去水印耗时：{}ms", endTime - startTime);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed("视频去水印失败");
    }

    @PostMapping("/text-to-video")
    @ApiOperation(value = "文本转视频", notes = "用户输入文本，后台将文本转换为视频")
    public ResponseResult<PromptDto> textToVideo(@Validated @RequestBody TextToVideoRequest param) throws IOException {
        val startTime = System.currentTimeMillis();
        val result = videoService.textToVideo(param);
        val endTime = System.currentTimeMillis();
        log.info("文本转视频耗时：{}ms", endTime - startTime);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed("文本转视频失败");
    }

    @PostMapping("/image-to-video")
    @ApiOperation(value = "图片转视频", notes = "用户输入图片，后台将图片转换为视频")
    public ResponseResult<PromptDto> imageToVideo(@Validated ImageToVideoRequest param, @RequestPart MultipartFile file)
            throws IOException {
        val startTime = System.currentTimeMillis();
        val result = videoService.imageToVideo(param, file);
        val endTime = System.currentTimeMillis();
        log.info("图片转视频耗时：{}ms", endTime - startTime);
        return result != null ? ResponseResult.success(result) : ResponseResult.failed("图片转视频失败");
    }

}
